package com.kylin.common.util;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.filter.SimplePropertyPreFilter;

/**
 * fastJson工具类
 *
 * @author wuhao
 * @version 1.0 - 2020/9/3
 */
public class JsonUtils {

    /**
     * 对象转为字符串
     * @param object 要转换的对象
     * @param fields 需要保留的字段
     * @return
     * @version 1.0  by wuhao at 2020/9/3
     */
    public static String filter(Object object, String... fields) {
        SimplePropertyPreFilter filter = new SimplePropertyPreFilter(object.getClass(), fields);
        return JSON.toJSONString(object,filter, null);
    }

    /**
     * list转为字符串
     * @param object list对象
     * @param clazz 对象类型
     * @param fields 需要保留的字段
     * @return
     * @version 1.0  by wuhao at 2020/9/3
     */
    public static String filter(Object object, Class clazz, String... fields) {
        SimplePropertyPreFilter filter = new SimplePropertyPreFilter(clazz, fields);
        return JSON.toJSONString(object,filter, null);
    }
}
